import styleList from '@/common/js/styleList.js';
import { getXAxis, getYAxis } from './common';

let commonEopts = {
  tooltip: {
    ...styleList['tooltip1']
  },
  grid: {
    left: '3%',
    right: '5%',
    containLabel: true,
  },
  legend: {
    itemWidth: 16,
    itemHeight: 10,
    bottom: 20,
    textStyle: {
      fontSize: 10,
      color: '#fff',
    },
  },
}

export const handleSecondChar = function (itemData, title) {
  // 处理预测值 每个都执行一遍
  supplement(itemData)
  itemData = handleTb(itemData)
  // 存在style为10的执行一遍
  handleData10(itemData)

  let getSeriesArr = getSeries(itemData, title)
  let series = getSeriesArr[0]
  let iszs = getSeriesArr[1]
  let chartData = {
    series,
  }
  let eopts = {
    ...JSON.parse(JSON.stringify(commonEopts)),

    xAxis: {
      ...getXAxis(itemData)
    },
    yAxis: getYAxis(itemData),
  }
  eopts.legend.data = itemData.map(e => {
    let obj = {
      name: e.data[0].targetName,
    }
    if (e.data[0].targetName.indexOf('正') != -1 || e.data[0].targetName.indexOf('负') != -1) {
      obj.itemStyle = {
        color: e.data[0].ifWarning
      }
    }
    return obj
  })
  return {
    chartData, eopts,
    iszs
  }
}

// 获取Series
function getSeries(itemData, title) {
  let lBarWidth = {}

  // 处理增速离群值
  let iszs = false
  const isTitleZS = (title.indexOf('增速') != -1)	
  let ltgt400 = false

  itemData.some(e => e.data.length > 13) ?
  lBarWidth.barWidth = "60%" :
  lBarWidth.barMaxWidth = 10

  let arr = itemData.map(e => {
    iszs = isTitleZS && (e.data[0].targetName.indexOf('增速') != -1 || e.data[0].targetName.indexOf('同比') != -1)
    let obj = {
      ...styleList[e.style],
      ...lBarWidth,
      barMinHeight: 3,
      name: e.data[0].targetName,
      yAxisIndex: e.needY ? 1 : 0,
      data: e.data.map(el => {
        if (iszs && el.targetValue > 400) {
          ltgt400 = true
          el.targetValue = 400
        } else if (iszs && el.targetValue < -400){
          ltgt400 = true
          el.targetValue = -400
        }
        if ( el.ifWarning && el.ifWarning.trim() != '' && el.ifWarning != '0') {
          return {
            value: el.targetValue,
            color: el.ifWarning,
            u: el.unit,
            itemStyle: {
              color: el.isOut ? '#1473a7' : el.ifWarning,
            }
          }
        }
        if (el.isOut) {
          return {
            value: el.targetValue,
            color: el.ifWarning,
            u: el.unit,
            itemStyle: {
              color: '#1473a7',
            }
          }
        } else {
          return {value: el.targetValue, u: el.unit}
        }
      })
    }
    if (e.data[0].targetName.indexOf('负') != -1) {
      obj.itemStyle = {
          borderRadius: [0,0,5,5]
      }
    }
    return obj
  })

  return [arr, iszs && ltgt400]
}

// 处理预测值
function supplement(itemData) {
  let arrxbl = itemData.find(e => e.style == 8)
  let arrxbtb = itemData.find(e => e.style == 3)
  if (!arrxbtb) return
  // isOut  true预测值   false实际值
  arrxbtb.data.forEach(e => {
    e.isOut = new Date(e.x) > new Date(e.reportDate) ? true : false
  })
  if (!arrxbl) return

  // 返回数据不够 显示有误 手动push数据
  let slicexbtb = arrxbtb.data.slice(arrxbl.data.length)
  slicexbtb.forEach(e => {
    arrxbl.data.push({
      codeFen: null,
      comFen: null,
      reportDate: e.reportDate,
      sortNum: 1,
      style: 8,
      targetId: null,
      targetName: "续保率",
      targetValue: null,
      unit: "%",
      x: e.x
    })
  })
}

// 处理不满数据情况
function handleData10(itemData) {
  itemData.forEach(e => {
    if (e.style == 10) {
      let length  = itemData.find(e => e.style != 10).data.length
      let arr = JSON.parse(JSON.stringify(e.data))
      for (let i = 0; i < length - 1; i++) {
        e.data.push(...arr)
      }
    }
  })
}

// 处理同比正 同比负
function handleTb(itemData) {
  let findTb = itemData.find(e => e.data[0].targetName == '同比' || e.data[0].targetName == '续保同比')
  if (findTb) {
    let tbz = []
    let tbf = []
    let allNum = []
    findTb.data.forEach(e => {
      allNum.push(e.targetValue)
      if ( e.targetValue >= 0) {
        tbz.push({
          ...e,
          targetName: e.targetName == '同比' ? '同比正' : '续保同比正',
          ifWarning: '#12b4ff'
        })
        tbf.push({
          ...e,
          targetValue: null,
          targetName: e.targetName == '同比' ?  '同比负' : '续保同比负',
          ifWarning: '#ffa97e'
        })
      } else {
        tbz.push({
          ...e,
          targetValue: null,
          targetName: e.targetName == '同比' ?  '同比正' : '续保同比正',
          ifWarning: '#12b4ff'
        })
        tbf.push({
          ...e,
          targetName: e.targetName == '同比' ?  '同比负' : '续保同比负',
          ifWarning: '#ffa97e'
        })
      }
    })

    let allTb = []

    if (Math.max(...allNum) >= 0) {   //存在同比正
      allTb.push({...findTb, data: tbz})
    }

    if (Math.min(...allNum) < 0) {   //存在同比负
      allTb.push({...findTb, data: tbf})
    }
    
    let ontTbList = itemData.filter(e => (e.data[0].targetName != '同比' && e.data[0].targetName != '续保同比'))
    console.log();
    return [...ontTbList, ...allTb]
  }
  return itemData
}